package net.oschina.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;

import net.oschina.app.R;
import net.oschina.app.adapter.BaseNewsBLogAdapter;
import net.oschina.app.base.BaseAdapter;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.bean.BlogBean;
import net.oschina.app.bean.GeneralBlogBean;
import net.oschina.app.bean.GeneralBlogDetailBean;
import net.oschina.app.bean.GeneralNewsBean;
import net.oschina.app.protocol.DetailNewsBlogProtocol;
import net.oschina.app.protocol.NewsBLogProtocol;
import net.oschina.app.ui.DetailActivity;
import net.oschina.app.util.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wei03 on 2017/1/14.
 */
public class NewsBlogFragment extends BaseFragment {
    private BaseNewsBLogAdapter mBaseNewsBLogAdapter;
    private ArrayList<GeneralNewsBean> mDatas = new ArrayList<>();
    private List<BlogBean> mBlogs;

    @Override
    protected BaseAdapter getRvAdapter() {
        mBaseNewsBLogAdapter = new BaseNewsBLogAdapter();
        return mBaseNewsBLogAdapter;
    }

    @Override
    protected void OnSubItemClick(RecyclerAdapterWithHF adapter, RecyclerView.ViewHolder vh, int position) {
        super.OnSubItemClick(adapter, vh, position);
        final Intent intent = new Intent(getContext(), DetailActivity.class);
        BlogBean blogBean = mBlogs.get(position);
        final int id = blogBean.getId();
        new Thread(new Runnable() {
            @Override
            public void run() {
                DetailNewsBlogProtocol detailNewsBlogProtocol = new DetailNewsBlogProtocol(id);
                GeneralBlogDetailBean generalBlogDetailBean = detailNewsBlogProtocol.getData(0);
                intent.putExtra("url", generalBlogDetailBean.getBlog().getUrl());
                intent.putExtra("title", getFragmentTitle());
                startActivity(intent);
            }
        }).start();

    }

    @Override
    protected String getFragmentTitle() {
        return getString(R.string.actionbar_title_blog);
    }

    @Override
    protected int onLoadSubData() {
        Bundle arguments = getArguments();
        String type="";
        if (arguments != null) {
            type = arguments.getString("type");
        }
        NewsBLogProtocol newsBLogProtocol = new NewsBLogProtocol(type);
        final GeneralBlogBean generalBlogBean =  newsBLogProtocol.getData(mDatas.size());
        if (generalBlogBean != null) {
            mPageIndex++;
            mBlogs = generalBlogBean.getBlogs();
            if (mBlogs.size() == 0 && mPageIndex == 0) {            //没有数据
                return STATE_EMPTY;
            }
            mList.addAll(mBlogs);
            CommonUtils.runOnMainThread(new Runnable() {
                @Override
                public void run() {
                    if (mBlogs.size() == 0) {
                        Toast.makeText(getContext(), "没有更多数据", Toast.LENGTH_SHORT).show();
                        ptrClassicFrameLayout.setLoadMoreEnable(false);
                    }
                    mBaseNewsBLogAdapter.notifyDataSetChanged();
                }
            });
            return STATE_SUCCESS;
        }
        return STATE_SUCCESS;
    }
}
